<?php
    session_start();
    include "../../function/functionPHP.php";
    include "../../class/crop.class.php";
    include "../../class/rice.class.php";
    include "../../class/farm.class.php";
    include "../../class/member.class.php";
    include "../../class/date.class.php";
    $mysqli=  connect();
    $cropID=$_POST["cropID"];
    
    $dateClass=new date();
    if(!is_array($cropID)){
        ?>
<div style="text-align: center;">กรุณาเลือก Crop</div>
            <?php
        exit();
    }else{
        foreach ($cropID as $key => $profitue) {
            $cropClass=new crop($profitue);
            $crop=$cropClass->getCrop();
            $startDate[]=$crop["startDate"];
            $farmClass=new farm($crop["farmID"]);
            $farm=$farmClass->getFarm();
            $memberClass=new member($farm["memID"]);
            $member=$memberClass->getMember();
            $memName[]=$memberClass->memName();
            
            $riceObj=new rice($crop["rvID"]);
            $rice=$riceObj->getRice();
            $rvName[]=$rice["rvName"];
            
            $profitTemp=$cropClass->profit();
            $profit[]=$profitTemp;
            
            $costTemp=$cropClass->sumCost();
            $cost[]=$costTemp;
            
            if($costTemp==0){
                $percent[]=0;
            }else{
                $percent[]=($profitTemp*100)/$costTemp;
            }
        }
    }
?>
<center>
    แผนภูมิแท่งเปรียบเทียบต้นทุน และรายได้เหนือต้นทุน<br>
</center>
<br>
<table class="noSpacing"style="width: 100%;height: 100%;">
    <tr>
        <td style="vertical-align: middle">
            <div style="border: none;margin-left: auto;margin-right: auto;display: table;">
                <table class="noSpacing"style="width: 100%;height: 100%;">
                    <tr>
                        <td style="vertical-align: middle">
                            <div style="margin-left: auto;margin-right: auto;display: table;">
                                <table class="noSpacing" style="margin-left: 30px;margin-right: 30px;width:90px;margin-top: 30px;">
                                    <tr>
                                        <td style="vertical-align: top;border-right: 1px solid #000;">
                                        </td>
                                        <?php
                                            $maxVal=max($profit);
                                            $maxHeight=250;
                                            
                                            $maxPercent=max($percent);
                                            for($i=0;$i<count($profit);$i++){
                                                $height=((100*$profit[$i])/$maxVal)*($maxHeight/100);
                                                //$height=$profit[$i]/14;
                                                
                                                $heightCost=((100*$cost[$i])/$maxVal)*($maxHeight/100);
                                                //$heightCost=$cost[$i]/14;
                                                
                                                if($cost[$i]==0){
                                                    $heightPercent=0;
                                                }else{
                                                    $heightPercent=((100*$percent[$i])/$maxPercent)*($maxHeight/100);
                                                    //$heightPercent=($profit[$i]/$cost[$i])*150;
                                                }
                                                
                                                //echo "Profit : ".$profit[$i]." : ".$height."<br>";
                                                ?>
                                        <td style="vertical-align: bottom;text-align: center;font-size: 60%;padding: 5px 0px 5px 5px;">
                                            <div 
                                                style="
                                                    position: relative;
                                                    background-color: #F00;
                                                    width: 30px;
                                                    height: <?=$heightCost?>px;
                                                    margin-left: auto;
                                                    margin-right: auto;"
                                                >
                                                <div style="
                                                    position: absolute;
                                                    color: #fff;
                                                    top: 25px;
                                                    left: -15px;
                                                    width: 60px;
                                                    text-align: right;
                                                    -ms-transform: rotate(-90deg); /* IE 9 */
                                                    -webkit-transform: rotate(-90deg); /* Chrome, Safari, Opera */
                                                    transform: rotate(-90deg);
                                                "><?=number_format($cost[$i]);?></div>
                                            </div>
                                        </td>
                                        <td style="vertical-align: bottom;text-align: center;font-size: 60%;padding: 5px 0px 5px 0px;">
                                            <div 
                                                style="
                                                    position: relative;
                                                    background-color: #428bca;
                                                    width: 30px;
                                                    height: <?=$height?>px;
                                                    margin-left: auto;
                                                    margin-right: auto;
                                                ">
                                                <div style="
                                                    position: absolute;
                                                    color: #fff;
                                                    top: 25px;
                                                    left: -15px;
                                                    width: 60px;
                                                    text-align: right;
                                                    -ms-transform: rotate(-90deg); /* IE 9 */
                                                    -webkit-transform: rotate(-90deg); /* Chrome, Safari, Opera */
                                                    transform: rotate(-90deg);
                                                "><?=  number_format($profit[$i])?></div>
                                            </div>
                                        </td>
                                        <td style="vertical-align: bottom;text-align: center;font-size: 60%;padding: 5px 5px 5px 0px;">
                                            <div 
                                                style="
                                                    position: relative;
                                                    background-color: #0f0;
                                                    width: 30px;
                                                    height: <?=$heightPercent?>px;
                                                    margin-left: auto;
                                                    margin-right: auto;
                                                ">
                                                <div style="
                                                    position: absolute;
                                                    color: #006200;
                                                    top: 20px;
                                                    left: -15px;
                                                    width: 60px;
                                                    font-weight: bold;
                                                    -ms-transform: rotate(-90deg); /* IE 9 */
                                                    -webkit-transform: rotate(-90deg); /* Chrome, Safari, Opera */
                                                    transform: rotate(-90deg);
                                                "><?php
                                                    if($cost[$i]!=0){
                                                        echo number_format($percent[$i])."%";
                                                    }
                                                ?></div>
                                            </div>
                                        </td>
                                                    <?php
                                            }
                                        ?>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <?php
                                            for($i=0;$i<count($profit);$i++){
                                                ?>
                                        <td colspan="3" style="vertical-align: bottom;text-align: center;font-size: 80%;padding: 5px;border-top: 1px solid #000;">
                                            <?=$i+1?>
                                        </td>
                                                    <?php
                                            }
                                        ?>
                                    </tr>
                                    <tr>
                                        <td colspan="<?=count($profit)*3?>" style="text-align: center;font-size: 80%;padding-bottom: 5px;">
                                            รายที่<br>
                                            <table style="margin-left: auto;text-align: left;">
                                                <tr>
                                                    <td style="padding: 5px;"><div style="height: 25px;width: 25px;background-color: #f00;float: left;margin-right: 5px;"></div></td>
                                                    <td style="padding: 5px;">ต้นทุน (บาท/ไร่)</td>
                                                </tr>
                                                <tr>
                                                    <td style="padding: 5px;"><div style="height: 25px;width: 25px;background-color: #428bca;float: left;margin-right: 5px;"></div></td>
                                                    <td style="padding: 5px;">รายได้เหนือต้นทุน (บาท/ไร่)</td>
                                                </tr>
                                                <tr>
                                                    <td style="padding: 5px;"><div style="height: 25px;width: 25px;background-color: #0f0;float: left;margin-right: 5px;"></div></td>
                                                    <td style="padding: 5px;">รายได้เหนือต้นทุน (%)</td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <div style="border: 1px solid #428bca;margin-top: 10px;margin-left: auto;margin-right: auto;display: table;padding: 0px;font-size: 80%;">
                <table class="noSpacing" style="">
                    <tr style="text-align: center;background-color: #428bca;color: #fff;">
                        <td style="padding: 5px 10px 5px 10px;">รายที่</td>
                        <td style="padding: 5px 10px 5px 10px;">ชื่อ นามสกุล / วันที่เริ่มปลูก</td>
                        <td style="width: 150px;padding: 5px 10px 5px 10px;">ต้นทุน (บาท/ไร่)</td>
                        <td style="text-align: center;padding: 5px 10px 5px 10px;">กำไร (บาท/ไร่)</td>
                        <td style="text-align: center;padding: 5px 10px 5px 10px;">กำไร (%)</td>
                    </tr>
                    <?php
                        for($i=0;$i<count($profit);$i++){
                            ?>
                    <tr>
                        <td style="padding: 5px 10px 5px 10px;text-align: center;border-bottom: 1px dashed #aaa;"><?=$i+1?></td>
                        <td style="padding: 5px 10px 5px 10px;border-bottom: 1px dashed #aaa;">
                            <?=$memName[$i]?> (<?=$rvName[$i]?>)
                            <div style="font-size: 80%;"><?=$dateClass->dateEncodeBE($startDate[$i])?></div>
                        </td>
                        <td style="padding: 5px 10px 5px 10px;text-align: right;border-bottom: 1px dashed #aaa;"><?=  number_format($cost[$i]);?></td>
                        <td style="padding: 5px 10px 5px 10px;text-align: right;border-bottom: 1px dashed #aaa;width: 100px;"><?=  number_format($profit[$i]);?></td>
                        <td style="padding: 5px 10px 5px 10px;text-align: right;border-bottom: 1px dashed #aaa;width: 100px;"><?=  number_format($percent[$i]);?>%</td>
                    </tr>
                                <?php
                        }
                    ?>
                </table>
            </div>
        </td>
    </tr>
</table>